﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Windows.Speech.PhraseRecognizer
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Runtime.CompilerServices;
using UnityEngine.Bindings;
using UnityEngine.Scripting;

#nullable disable
namespace UnityEngine.Windows.Speech
{
  /// <summary>
  ///   <para>A common base class for both keyword recognizer and grammar recognizer.</para>
  /// </summary>
  public abstract class PhraseRecognizer : IDisposable
  {
    protected IntPtr m_Recognizer;

    [NativeHeader("PlatformDependent/Win/Bindings/SpeechBindings.h")]
    [NativeThrows]
    [MethodImpl(MethodImplOptions.InternalCall)]
    protected static extern IntPtr CreateFromKeywords(
      object self,
      [Unmarshalled] string[] keywords,
      ConfidenceLevel minimumConfidence);

    [NativeThrows]
    [NativeHeader("PlatformDependent/Win/Bindings/SpeechBindings.h")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    protected static extern IntPtr CreateFromGrammarFile(
      object self,
      string grammarFilePath,
      ConfidenceLevel minimumConfidence);

    [NativeHeader("PlatformDependent/Win/Bindings/SpeechBindings.h")]
    [NativeThrows]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void Start_Internal(IntPtr recognizer);

    [NativeHeader("PlatformDependent/Win/Bindings/SpeechBindings.h")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void Stop_Internal(IntPtr recognizer);

    [NativeHeader("PlatformDependent/Win/Bindings/SpeechBindings.h")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern bool IsRunning_Internal(IntPtr recognizer);

    [NativeHeader("PlatformDependent/Win/Bindings/SpeechBindings.h")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void Destroy(IntPtr recognizer);

    [NativeHeader("PlatformDependent/Win/Bindings/SpeechBindings.h")]
    [ThreadSafe]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void DestroyThreaded(IntPtr recognizer);

    public event PhraseRecognizer.PhraseRecognizedDelegate OnPhraseRecognized;

    internal PhraseRecognizer()
    {
    }

    ~PhraseRecognizer()
    {
      if (!(this.m_Recognizer != IntPtr.Zero))
        return;
      PhraseRecognizer.DestroyThreaded(this.m_Recognizer);
      this.m_Recognizer = IntPtr.Zero;
      GC.SuppressFinalize((object) this);
    }

    /// <summary>
    ///   <para>Makes the phrase recognizer start listening to phrases.</para>
    /// </summary>
    public void Start()
    {
      if (this.m_Recognizer == IntPtr.Zero)
        return;
      PhraseRecognizer.Start_Internal(this.m_Recognizer);
    }

    /// <summary>
    ///   <para>Stops the phrase recognizer from listening to phrases.</para>
    /// </summary>
    public void Stop()
    {
      if (this.m_Recognizer == IntPtr.Zero)
        return;
      PhraseRecognizer.Stop_Internal(this.m_Recognizer);
    }

    /// <summary>
    ///   <para>Disposes the resources used by phrase recognizer.</para>
    /// </summary>
    public void Dispose()
    {
      if (this.m_Recognizer != IntPtr.Zero)
      {
        PhraseRecognizer.Destroy(this.m_Recognizer);
        this.m_Recognizer = IntPtr.Zero;
      }
      GC.SuppressFinalize((object) this);
    }

    /// <summary>
    ///   <para>Tells whether the phrase recognizer is listening for phrases.</para>
    /// </summary>
    public bool IsRunning
    {
      get
      {
        return this.m_Recognizer != IntPtr.Zero && PhraseRecognizer.IsRunning_Internal(this.m_Recognizer);
      }
    }

    [RequiredByNativeCode]
    private void InvokePhraseRecognizedEvent(
      string text,
      ConfidenceLevel confidence,
      SemanticMeaning[] semanticMeanings,
      long phraseStartFileTime,
      long phraseDurationTicks)
    {
      PhraseRecognizer.PhraseRecognizedDelegate phraseRecognized = this.OnPhraseRecognized;
      if (phraseRecognized == null)
        return;
      phraseRecognized(new PhraseRecognizedEventArgs(text, confidence, semanticMeanings, DateTime.FromFileTime(phraseStartFileTime), TimeSpan.FromTicks(phraseDurationTicks)));
    }

    [RequiredByNativeCode]
    private static unsafe SemanticMeaning[] MarshalSemanticMeaning(
      IntPtr keys,
      IntPtr values,
      IntPtr valueSizes,
      int valueCount)
    {
      SemanticMeaning[] semanticMeaningArray = new SemanticMeaning[valueCount];
      int num = 0;
      for (int index1 = 0; index1 < valueCount; ++index1)
      {
        uint length = *(uint*) ((IntPtr) (void*) valueSizes + (IntPtr) index1 * 4);
        SemanticMeaning semanticMeaning = new SemanticMeaning()
        {
          key = new string(((char**) (void*) keys)[index1]),
          values = new string[(int) length]
        };
        for (int index2 = 0; (long) index2 < (long) length; ++index2)
          semanticMeaning.values[index2] = new string(((char**) (void*) values)[num + index2]);
        semanticMeaningArray[index1] = semanticMeaning;
        num += (int) length;
      }
      return semanticMeaningArray;
    }

    /// <summary>
    ///   <para>Delegate for OnPhraseRecognized event.</para>
    /// </summary>
    /// <param name="args">Information about a phrase recognized event.</param>
    public delegate void PhraseRecognizedDelegate(PhraseRecognizedEventArgs args);
  }
}
